package com.eastfair.config.service.entityservice.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.eastfair.config.entity.ConfigAttributeValue;
import com.eastfair.config.entity.LaunchPicture;
import com.eastfair.config.service.ConfigAttributeValueService;
import com.eastfair.config.service.entityservice.LaunchPictureService;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.utils.StringUtils;
import com.eastfair.annotation.base.LanguageTypeEnum;
import com.eastfair.enums.TerminalTypeEnum;
import com.eastfair.projectcore.api.ProjectCoreServiceFeign;import com.eastfair.core.snowflake.DefaultUidGenerator;
import com.eastfair.projectcore.dto.ConfigAttributeDTO;
import com.eastfair.projectcore.vo.ConfigAttributeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.eastfair.core.exception.code.ExceptionCode.ERROR_NULL_ID;
import static com.eastfair.core.exception.code.ExceptionCode.ERROR_NULL_PARAMS;

/**
 * 启动闪图（闪屏）服务
 * Date:2021/8/7
 * Time:15:45
 *
 * @author zjn
 */
@Service
public class LaunchPictureServiceImpl implements LaunchPictureService {

    @Autowired
    private ProjectCoreServiceFeign projectCoreServiceFeign;
    @Autowired
    private ConfigAttributeValueService configAttributeValueService;

    @Override
    public List<ConfigAttributeVO> getAttributes() {
        ConfigAttributeDTO configAttributeDTO = new ConfigAttributeDTO();
        configAttributeDTO.setEntityClass(LaunchPicture.class.getName());
        return projectCoreServiceFeign.listByEntityClass(configAttributeDTO).getData();
    }

    @Override
    public List<LaunchPicture> getList(LaunchPicture launchPicture) {
        List<LaunchPicture> launchPictureList = configAttributeValueService.listEntityByClass(LaunchPicture.class);
        if (launchPictureList == null || launchPictureList.isEmpty()) {
            return null;
        }
        if (Objects.isNull(launchPicture)) {
            return launchPictureList;
        }
        // 选择性过滤
        return launchPictureList.stream().filter(s -> compare(s, launchPicture)).collect(Collectors.toList());
    }

    @Override
    public R<LaunchPicture> get(Long flagId) {
        if (Objects.isNull(flagId)) {
            return R.fail(ERROR_NULL_ID);
        }
        LaunchPicture launchPicture = LaunchPicture.builder().flagId(flagId).build();
        List<LaunchPicture> launchPictureList = getList(launchPicture);
        if (launchPictureList != null && !launchPictureList.isEmpty()) {
            return R.success(launchPictureList.get(0));
        }
        return R.success(null);
    }

    @Override
    public R<LaunchPicture> getByEntity(LaunchPicture launchPicture) {
        if (StringUtils.isBlank(launchPicture.getLanguageType(), launchPicture.getTerminalType()) || Objects.isNull(launchPicture.getExhibitionId())) {
            return R.fail(ERROR_NULL_PARAMS);
        }

        LaunchPicture param = LaunchPicture.builder().exhibitionId(launchPicture.getExhibitionId()).languageType(LanguageTypeEnum.get(launchPicture.getLanguageType()).name())
                .terminalType(TerminalTypeEnum.get(launchPicture.getTerminalType()).getCode()).build();

        List<LaunchPicture> launchPictureList = getList(param);
        if (launchPictureList != null && !launchPictureList.isEmpty()) {
            return R.success(launchPictureList.get(0));
        }
        return R.success(null);
    }

    @Override
    public R<Boolean> saveEntity(LaunchPicture launchPicture) {
        if (StringUtils.isBlank(launchPicture.getName(), launchPicture.getIconUrl())) {
            return R.fail(ERROR_NULL_PARAMS);
        }

        return R.success(configAttributeValueService.saveEntity(launchPicture));
    }

    @Override
    public R<Boolean> deleteEntity(Long flagId) {
        if (Objects.isNull(flagId)) {
            return R.fail(ERROR_NULL_ID);
        }

        UpdateWrapper<ConfigAttributeValue> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(ConfigAttributeValue::getProjectId, ContextUtil.getProjectId())
                .eq(ConfigAttributeValue::getFlagId, flagId).set(ConfigAttributeValue::getIsDelete, BusinessConstant.DELETE_YES);
        return R.success(configAttributeValueService.update(updateWrapper));
    }

    @Override
    public R<Boolean> updateEntity(LaunchPicture launchPicture) {
        if (Objects.isNull(launchPicture.getFlagId())) {
            return R.fail(ERROR_NULL_ID);
        }
        if (StringUtils.isBlank(launchPicture.getName()) && StringUtils.isBlank(launchPicture.getIconUrl())) {
            return R.fail(ERROR_NULL_PARAMS);
        }

        return R.success(configAttributeValueService.updateEntity(launchPicture));
    }

    /**
     * 启动闪图（闪屏）过滤器
     *
     * @param target   目标值
     * @param standard 过滤条件
     * @return
     */
    private Boolean compare(LaunchPicture target, LaunchPicture standard) {
        if (Objects.isNull(target) || Objects.isNull(standard)) {
            return true;
        }

        if (!Objects.isNull(standard.getFlagId())) {
            if (!standard.getFlagId().equals(target.getFlagId())) {
                return false;
            }
        }

        if (!Objects.isNull(standard.getExhibitionId())) {
            if (!standard.getExhibitionId().equals(target.getExhibitionId())) {
                return false;
            }
        }

        if (!StringUtils.isBlank(standard.getName())) {
            if (StringUtils.isBlank(target.getName()) || !target.getName().contains(standard.getName())) {
                return false;
            }
        }

        if (!StringUtils.isBlank(standard.getTerminalType())) {
            if (!standard.getTerminalType().equals(target.getTerminalType())) {
                return false;
            }
        }

        if (!StringUtils.isBlank(standard.getLanguageType())) {
            if (!standard.getLanguageType().equals(target.getLanguageType())) {
                return false;
            }
        }

        if (!Objects.isNull(standard.getIsEnabled())) {
            if (!standard.getIsEnabled().equals(target.getIsEnabled())) {
                return false;
            }
        }

        return true;
    }
}